package cn.xyinping.org.controller;

import cn.xyinping.basic.util.AjaxResult;
import cn.xyinping.basic.util.PageList;
import cn.xyinping.org.domain.Employee;
import cn.xyinping.org.domain.SystemDictionaryType;
import cn.xyinping.org.query.SystemDictionaryTypeQuery;
import cn.xyinping.org.service.ISystemDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 27266
 * @desrciption 数据字典表
 * @date 2021/10/25
 */
@RestController
@RequestMapping("/type")
public class SystemDictionaryTypeController {
    @Autowired
    private ISystemDictionaryTypeService typeService;
    // 查一条数据
    @GetMapping("/{id}")
    public SystemDictionaryType queryById(@PathVariable("id") long id){
        return typeService.findOne(id);
    }
    // 查询所有数据
    @GetMapping
    public List<SystemDictionaryType> queryAll(){
        return typeService.findAll();
    }
    // 分页查询
    @PostMapping
    public PageList<SystemDictionaryType> queryPage(@RequestBody SystemDictionaryTypeQuery query){
        return typeService.queryList(query);
    }
    // 删除
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id") long id){
        try {
            typeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙。。"); // 失败
        }
    }
    // 添加和修改
    @PutMapping
    public AjaxResult addAndUpdate(@RequestBody SystemDictionaryType systemDictionaryType){
        try {
            if(systemDictionaryType.getId() == null){
                typeService.add(systemDictionaryType);
            }else {
                typeService.update(systemDictionaryType);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙。。。");
        }
    }


}
